package com.kikt.recyclerview.loadmore

import android.os.Handler
import android.os.Looper
import android.support.v4.widget.SwipeRefreshLayout
import com.jaychang.srv.SimpleCell
import com.jaychang.srv.SimpleRecyclerView
import com.jaychang.srv.SimpleViewHolder
import java.util.concurrent.Executors

/**
 * Created by cai on 2017/10/27.
 */
class SimpleRecyclerViewHelper<Data, VH : SimpleViewHolder?>(val simpleRecyclerView: SimpleRecyclerView,
                                                             val view: RvListView<Data, VH>,
                                                             val dataGetter: IDataGetter<Data, VH>,
                                                             var refreshLayout: SwipeRefreshLayout? = null) {

    val delegate = RvListDelegate(view)

    private var refreshIng = false
        set(value) {
            field = value
            refreshLayout?.isRefreshing = value
        }

    private var loadmoreIng = false
        set(value) {
            field = value
            simpleRecyclerView.setLoadingMore(value)
        }

    /**
     * 获取当前是否正在刷新/加载数据
     */
    fun isRefreshOrLoading(): Boolean {
        return refreshIng || loadmoreIng
    }

    init {
        dataGetter.bindView(delegate)

        simpleRecyclerView.setOnLoadMoreListener {
            if (!isRefreshOrLoading()) {
                loadMoreData()
            }
        }

        refreshLayout?.setOnRefreshListener {
            if (!isRefreshOrLoading()) {
                getFirstData()
            }
        }
    }

    inner class RvListDelegate(val rvListView: RvListView<Data, VH>) : RvListView<Data, VH> by rvListView {

        override fun onDataList(list: List<Data>) {
            onDataGet(list)
            loadmoreIng = false
            refreshIng = false
        }

    }

    fun onDataGet(list: List<Data>) {
        val cells = ArrayList<SimpleCell<*, *>>()
        list.forEach {
            val cell: SimpleCell<*, *> = view.createCell(it)
            cells.add(cell)
        }
        simpleRecyclerView.addCells(cells)
    }

    fun loadMoreData() {
        loadmoreIng = true
        dataGetter.loadMoreData()
    }

    fun getFirstData() {
        refreshIng = true
        dataGetter.refreshData()
    }

    fun clearCells() {
        simpleRecyclerView.removeAllCells()
    }

}

interface DataGetter {

    /**
     * 刷新/重新加载的数据
     */
    fun loadMoreData()

    fun refreshData()

}

interface RvListView<Data, out VH : SimpleViewHolder?> {
    val helper: SimpleRecyclerViewHelper<Data, out VH>

    fun onDataList(list: List<Data>)

    fun createCell(data: Data): SimpleCell<Data, out VH>

    fun clearCells() {
        helper.clearCells()
    }
}

interface IDataGetter<Data, out VH : SimpleViewHolder?> : DataGetter {

    val view: RvListView<Data, VH>

    fun onGetData(list: List<Data>, clearData: Boolean = false) {
        if (clearData) {
            view.clearCells()
        }
        if (delegate != null) {
            delegate?.onDataList(list)
        } else {
            view.onDataList(list)
        }
    }

    fun clearData()

    var delegate: SimpleRecyclerViewHelper<Data, VH>.RvListDelegate?

    fun bindView(delegate: SimpleRecyclerViewHelper<Data, VH>.RvListDelegate) {
        this.delegate = delegate
    }

}


val threadPool = Executors.newCachedThreadPool()

fun async(runnable: () -> Unit) {
    threadPool.execute(runnable)
}

private val handler: Handler = Handler(Looper.getMainLooper())

fun runOnUiThread(runnable: () -> Unit) {
    handler.post(runnable)
}

fun runDelay(delayed: Long, runnable: () -> Unit) {
    handler.postDelayed(runnable, delayed)
}

